package cn.study.reentrantlock;

import java.util.concurrent.locks.ReentrantLock;

/**
 * @Author     ：跟大佳一起学架构
 * @Description：互斥锁的简单demo
 */
public class LockTest {
    // 通过true、false来控制是否是公平锁
    private static final ReentrantLock lock = new ReentrantLock(false); // ...
//    private static final SelfReentrantLock lock = new SelfReentrantLock(); // ...

    static class LockRunnable implements Runnable {
        /**
         * @Description： 互斥两秒执行任务
         */
        public void run(){
            lock.lock();  // block until condition holds
            try {
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println("两个互斥2S执行");
                // ... method body
            } finally {
                lock.unlock();
            }
        }
    }

    public static void main(String[] args) {
        new Thread(new LockRunnable(),"lock1").start();
        new Thread(new LockRunnable(),"lock2").start();
    }
}
